'20821 -75
hatchAngle  = 45                  
'hatchStep= 1                  

'PAR_d=40
'PAR_DD=68
'PAR_H=36
'PAR_C=18
'PAR_C1=9
'PAR_d1=58.5
'PAR_r = 1.5
'PAR_r1 = 0.3

Const POINT_ARR = 2  
Const ko_MathPointParam = 14 

DIM math,iDA,iPoint,aPar1(7)

''PAR_AXIS = 1
'++++
SUB LINE (x1,y1,x2,y2,t) Doc.ksLineSEG x1,y1,x2,y2,t END SUB
Sub ARC  (xc,yc,rad,x1,y1,x2,y2,Dir,style) doc.ksArcByPoint xc,yc,rad,x1,y1,x2,y2,Dir,style End Sub 
Sub Circle (xc,yc,rad,style) doc.ksCircle xc,yc,rad, style End Sub 
'++++
Sub Script_OnLoad ()

  Set iDA = Kompas.GetDynamicArray(POINT_ARR)
  Set iPoint = Kompas.GetParamStruct(ko_MathPointParam)
  Set math = Kompas.GetMathematic2D

d1=PAR_d1*0.5
d=PAR_d*0.5
DD=PAR_DD*0.5
C=PAR_C
C1=PAR_C1
H=PAR_H
r=PAR_r
r1=PAR_r1

rad =c1*0.35

Xc1 = (H-C)*0.5
Yc1 =(d+d1+rad*1.4)*0.5

app DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,1
DrawCentr DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,1
DrawCentr DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,-1

hatchAngle=hatchAngle-90
DrawRing d1,d,c1,r,r1,Xc1,Yc1,rad,1
DrawRing d1,d,c1,r,r1,Xc1,Yc1,rad,-1
hatchAngle=45
Doc.ksMtr H,0 ,180, 1 ,1
DrawRing d1,d,c1,r,r1,Xc1,Yc1,rad,1
DrawRing d1,d,c1,r,r1,Xc1,Yc1,rad,-1
Doc.KsDeleteMtr 

If PAR_AXIS =1 Then 
Line H*0.5,DD+2,H*0.5,d-2,3
Line -2,0,H+2,0,3
End If 
End Sub
'++++
Sub DrawRing (d1,d,c1,r,r1,Xc1,Yc1,rad,ky)
Dim xp(2),yp(2)

If PAR_AXIS = 1 Then 
Line 0,0,0,(d1-r1)*ky,1
Line r,0,r,d*ky,1
Line c1,0,c1,(d+r)*ky,1
Line c1-r,0,c1-r,d*ky,1
Else 
Line 0,(d1-r1)*ky,0,(d+r)*ky,1
End If 


Line r,d*ky,c1-r,d*ky,1
arc  r,(d+r)*ky,r,0,(d+r)*ky,r,d*ky,ky,1
arc  c1-r,(d+r)*ky,r,c1-r,d*ky,c1,(d+r)*ky,ky,1
arc r1,(d1-r1)*ky,r1,r1,d1*ky,0,(d1-r1)*ky,ky,1
Line c1,(d+r)*ky,c1,(d+1.5*r)*ky,1
Line r1,d1*ky,c1*0.5,d1*ky,1

Circle xc1,yc1*ky,rad,1
Line xc1-rad*1.1,yc1*ky ,xc1+rad*1.1,yc1*ky,3
Line xc1,(yc1-rad*1.1)*ky ,xc1,(yc1+rad*1.1)*ky,3
 iDA.ksClearArray
 math.ksIntersectLinSCir c1*0.5,d1*ky,c1,(d+1.5*r)*ky,xc1,yc1*ky,rad,iDA
  i= iDA.ksGetArrayCount 
  If i >0 Then 
    iDA.ksGetArrayItem 0, iPoint
     xp(0) = iPoint.x
     yp(0) = iPoint.y
    If i>1 Then 
     iDA.ksGetArrayItem 1, iPoint
     xp(1) = iPoint.x
     yp(1) = iPoint.y
 Line c1*0.5,d1*ky,xp(1),yp(1),1
 Line xp(0),yp(0),c1,(d+1.5*r)*ky,1
  End If 
  Else 
  Line c1*0.5,d1*ky,c1,(d+1.5*r)*ky,1	
 End If 
 
Doc.ksHatch 0, hatchAngle , hatchStep,0,0,0
 Line r,d*ky,c1-r,d*ky,1
 arc  r,(d+r)*ky,r,0,(d+r)*ky,r,d*ky,ky,1
 arc  c1-r,(d+r)*ky,r,c1-r,d*ky,c1,(d+r)*ky,ky,1
 arc r1,(d1-r1)*ky,r1,r1,d1*ky,0,(d1-r1)*ky,ky,1
 Line r1,d1*ky,c1*0.5,d1*ky,1
 Line 0,(d+r)*ky,0,(d1-r1)*ky,1
 Line c1,(d+r)*ky,c1,(d+1.5*r)*ky,1
 If i>1 Then 
  Line c1*0.5,d1*ky,xp(1),yp(1),1
  Line xp(0),yp(0),c1,(d+1.5*r)*ky,1
  arc xc1,yc1*ky,rad,xp(1),yp(1),xp(0),yp(0),ky,1
  Else 
   Line c1*0.5,d1*ky,c1,(d+1.5*r)*ky,1	
  End If  
Doc.ksEndObj
End Sub 
'++++
Sub DrawCentr (DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,ky)
Dim xp(3),yp(3)
Line c1,(d+r*0.8)*ky,c1,(d+1.5*r)*ky,1
Line H-c1,(d+r*0.8)*ky,H-c1,(d+1.5*r)*ky,1
Line c1,(d+r*0.8)*ky,H-c1,(d+r*0.8)*ky,1
Line c1,(d+1.5*r)*ky,H-c1,(d+1.5*r)*ky,1
Doc.ksHatch 0, hatchAngle , hatchStep,0,0,0
Line c1,(d+r*0.8)*ky,c1,(d+1.5*r)*ky,1
Line H-c1,(d+r*0.8)*ky,H-c1,(d+1.5*r)*ky,1
Line c1,(d+r*0.8)*ky,H-c1,(d+r*0.8)*ky,1
Line c1,(d+1.5*r)*ky,H-c1,(d+1.5*r)*ky,1
Doc.ksEndObj


Line Xc1+r1,DD*ky,(H-rad*1.5)*0.5,DD*ky,1
Line (H+rad*1.5)*0.5,DD*ky,Xc1+C-r1,DD*ky,1
doc.ksArcBy3Points (H-rad*1.5)*0.5,DD*ky,H*0.5,(DD-3*r1)*ky,(H+rad*1.5)*0.5,DD*ky,1
arc Xc1+r1,(DD-r1)*ky,r1,Xc1+r1,DD*ky,Xc1,(DD-r1)*ky,ky,1
arc Xc1+C-r1,(DD-r1)*ky,r1,Xc1+C,(DD-r1)*ky,Xc1+C-r1,DD*ky,ky,1
Line Xc1,(DD-r1)*ky,Xc1,(DD-r1-r)*ky,1
Line Xc1+C,(DD-r1)*ky,Xc1+C,(DD-r1-r)*ky,1


ang = Math.ksAngle(c1*0.5,d1*ky,c1,(d+1.5*r)*ky)

iDA.ksClearArray
 math.ksIntersectLinSLine Xc1,(yc1-r1)*ky,Xc1+C,(yc1-r1)*ky,Xc1,(DD-r1-r)*ky,ang,iDA
 iDA.ksGetArrayItem 0, iPoint
     xp(2) = iPoint.x
     yp(2) = iPoint.y

Line xp(2),yp(2),H-xp(2),yp(2),1

 iDA.ksClearArray
 math.ksIntersectLinSCir  Xc1,(DD-r1-r)*ky,xp(2),yp(2),xc1,yc1*ky,rad,iDA
  i= iDA.ksGetArrayCount 
  If i >0 Then 
    iDA.ksGetArrayItem 0, iPoint
     xp(0) = iPoint.x
     yp(0) = iPoint.y
    If i>1 Then 
     iDA.ksGetArrayItem 1, iPoint
     xp(1) = iPoint.x
     yp(1) = iPoint.y
 Line Xc1,(DD-r1-r)*ky,xp(1),yp(1),1
 Line xp(0),yp(0),xp(2),yp(2),1
 
 Line Xc1+C,(DD-r1-r)*ky,H-xp(1),yp(1),1
 Line H-xp(0),yp(0),H-xp(2),yp(2),1
  End If 
  Else 
  Line  Xc1,(DD-r1-r)*ky,xp(2),yp(2),1	
  Line  Xc1+C,(DD-r1-r)*ky,H-xp(2),yp(2),1	
 End If 
 If ky =-1 Then 
Doc.ksHatch 0, hatchAngle , hatchStep,0,0,0
Line Xc1+r1,DD*ky,(H-rad*1.5)*0.5,DD*ky,1
Line (H+rad*1.5)*0.5,DD*ky,Xc1+C-r1,DD*ky,1
doc.ksArcBy3Points (H-rad*1.5)*0.5,DD*ky,H*0.5,(DD-3*r1)*ky,(H+rad*1.5)*0.5,DD*ky,1
arc Xc1+r1,(DD-r1)*ky,r1,Xc1+r1,DD*ky,Xc1,(DD-r1)*ky,ky,1
arc Xc1+C-r1,(DD-r1)*ky,r1,Xc1+C,(DD-r1)*ky,Xc1+C-r1,DD*ky,ky,1
Line Xc1,(DD-r1)*ky,Xc1,(DD-r1-r)*ky,1
Line Xc1+C,(DD-r1)*ky,Xc1+C,(DD-r1-r)*ky,1
Line xp(2),yp(2),H-xp(2),yp(2),1
If i>1 Then 
 Line Xc1,(DD-r1-r)*ky,xp(1),yp(1),1
 Line xp(0),yp(0),xp(2),yp(2),1
 Line Xc1+C,(DD-r1-r)*ky,H-xp(1),yp(1),1
 Line H-xp(0),yp(0),H-xp(2),yp(2),1
 arc xc1,yc1*ky,rad,xp(0),yp(0),xp(1),yp(1),ky,1
 arc H-xc1,yc1*ky,rad,H-xp(0),yp(0),H-xp(1),yp(1),-ky,1
 Else 
  Line  Xc1,(DD-r1-r)*ky,xp(2),yp(2),1	
  Line  Xc1+C,(DD-r1-r)*ky,H-xp(2),yp(2),1	
 End If 
Doc.ksEndObj
Else 
 ksGetParamArc3Point (H-rad*1.5)*0.5,DD*ky,H*0.5,(DD-3*r1)*ky,(H+rad*1.5)*0.5,DD*ky,aPar1
iDA.ksClearArray
  math.ksIntersectLinsCir (H-rad)*0.5,DD*ky,(H-rad)*0.5,yp(2), aPar1(1),aPar1(2),aPar1(3),iDA
  j = iDA.ksGetArrayCount
  If  j <>0 Then   
   iDA.ksGetArrayItem 0, iPoint
   yy = iPoint.y
  End If
 Line (H-rad)*0.5,yy,(H-rad)*0.5,yp(2),1
 Line (H+rad)*0.5,yy,(H+rad)*0.5,yp(2),1
 
Doc.ksHatch 0, hatchAngle , hatchStep,0,0,0
Line Xc1+r1,DD*ky,(H-rad*1.5)*0.5,DD*ky,1
arc aPar1(1),aPar1(2),aPar1(3),(H-rad*1.5)*0.5,DD*ky,(H-rad)*0.5,yy,ky,1
arc Xc1+r1,(DD-r1)*ky,r1,Xc1+r1,DD*ky,Xc1,(DD-r1)*ky,ky,1
Line Xc1,(DD-r1)*ky,Xc1,(DD-r1-r)*ky,1
Line xp(2),yp(2),(H-rad)*0.5,yp(2),1
Line (H-rad)*0.5,yy,(H-rad)*0.5,yp(2),1
If i>1 Then 
 Line Xc1,(DD-r1-r)*ky,xp(1),yp(1),1
 Line xp(0),yp(0),xp(2),yp(2),1
 arc xc1,yc1*ky,rad,xp(0),yp(0),xp(1),yp(1),ky,1
 Else 
  Line  Xc1,(DD-r1-r)*ky,xp(2),yp(2),1	
 End If 
Doc.ksEndObj 
Doc.ksHatch 0, hatchAngle , hatchStep,0,0,0
Line (H+rad*1.5)*0.5,DD*ky,Xc1+C-r1,DD*ky,1
arc aPar1(1),aPar1(2),aPar1(3),(H+rad*1.5)*0.5,DD*ky,(H+rad)*0.5,yy,-ky,1
arc Xc1+C-r1,(DD-r1)*ky,r1,Xc1+C,(DD-r1)*ky,Xc1+C-r1,DD*ky,ky,1
Line Xc1+C,(DD-r1)*ky,Xc1+C,(DD-r1-r)*ky,1
Line H-xp(2),yp(2),(H+rad)*0.5,yp(2),1
Line (H+rad)*0.5,yy,(H+rad)*0.5,yp(2),1
If i>1 Then 
  Line Xc1+C,(DD-r1-r)*ky,H-xp(1),yp(1),1
  Line H-xp(0),yp(0),H-xp(2),yp(2),1
  arc H-xc1,yc1*ky,rad,H-xp(0),yp(0),H-xp(1),yp(1),-ky,1
Else 
 Line  Xc1+C,(DD-r1-r)*ky,H-xp(2),yp(2),1	
 End If 	
Doc.ksEndObj 
End If 
End Sub 
'++++
Sub Contur(d1,d,c1,r,r1,Xc1,Yc1,rad,ky)
Dim xp(2),yp(2)
Line 0,0,0,(d1-r1)*ky,1
arc r1,(d1-r1)*ky,r1,r1,d1*ky,0,(d1-r1)*ky,ky,1
Line r1,d1*ky,c1*0.5,d1*ky,1

 iDA.ksClearArray
 math.ksIntersectLinSCir c1*0.5,d1*ky,c1,(d+1.5*r)*ky,xc1,yc1*ky,rad,iDA
  i= iDA.ksGetArrayCount 
  If i >0 Then 
    iDA.ksGetArrayItem 0, iPoint
     xp(0) = iPoint.x
     yp(0) = iPoint.y
    If i>1 Then 
     iDA.ksGetArrayItem 1, iPoint
     xp(1) = iPoint.x
     yp(1) = iPoint.y
 Line c1*0.5,d1*ky,xp(1),yp(1),1
  End If 
  Else 
  Line c1*0.5,d1*ky,c1,(d+1.5*r)*ky,1	
 End If 
End Sub 
'+++
Sub APP (DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,ky)
doc.ksColouring &HFE000000
Contur d1,d,c1,r,r1,Xc1,Yc1,rad,1
Contur d1,d,c1,r,r1,Xc1,Yc1,rad,-1
Doc.ksMtr H,0 ,180, 1 ,1
Contur d1,d,c1,r,r1,Xc1,Yc1,rad,1
Contur d1,d,c1,r,r1,Xc1,Yc1,rad,-1
Doc.KsDeleteMtr 
ConturRing DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,1 
ConturRing DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,-1 
doc.ksEndObj
End Sub 
'+++
Sub ConturRing (DD,d,d1,c,c1,H,r,r1,Xc1,Yc1,rad,ky)   
Dim xp(3),yp(3)
Line Xc1+r1,DD*ky,(H-rad*1.5)*0.5,DD*ky,1
Line (H+rad*1.5)*0.5,DD*ky,Xc1+C-r1,DD*ky,1
doc.ksArcBy3Points (H-rad*1.5)*0.5,DD*ky,H*0.5,(DD-3*r1)*ky,(H+rad*1.5)*0.5,DD*ky,1
arc Xc1+r1,(DD-r1)*ky,r1,Xc1+r1,DD*ky,Xc1,(DD-r1)*ky,ky,1
arc Xc1+C-r1,(DD-r1)*ky,r1,Xc1+C,(DD-r1)*ky,Xc1+C-r1,DD*ky,ky,1
Line Xc1,(DD-r1)*ky,Xc1,(DD-r1-r)*ky,1
Line Xc1+C,(DD-r1)*ky,Xc1+C,(DD-r1-r)*ky,1


ang = Math.ksAngle(c1*0.5,d1*ky,c1,(d+1.5*r)*ky)

iDA.ksClearArray
 math.ksIntersectLinSLine Xc1,(yc1-r1)*ky,Xc1+C,(yc1-r1)*ky,Xc1,(DD-r1-r)*ky,ang,iDA
 iDA.ksGetArrayItem 0, iPoint
     xp(2) = iPoint.x
     yp(2) = iPoint.y

 iDA.ksClearArray
 math.ksIntersectLinSCir  Xc1,(DD-r1-r)*ky,xp(2),yp(2),xc1,yc1*ky,rad,iDA
  i= iDA.ksGetArrayCount 
 If i>1 Then 
     iDA.ksGetArrayItem 1, iPoint
     xp(1) = iPoint.x
     yp(1) = iPoint.y
 Line Xc1,(DD-r1-r)*ky,xp(1),yp(1),1
 Line Xc1+C,(DD-r1-r)*ky,H-xp(1),yp(1),1
  Else 
   Line  Xc1,(DD-r1-r)*ky,xp(2),yp(2),1	
   Line  Xc1+C,(DD-r1-r)*ky,H-xp(2),yp(2),1	
 End If 
 
 iDA.ksClearArray
 math.ksIntersectLinSCir c1*0.5,d1*ky,c1,(d+1.5*r)*ky,xc1,yc1*ky,rad,iDA
  i= iDA.ksGetArrayCount 
   If i >0 Then 
     iDA.ksGetArrayItem 1, iPoint
     xp(2) = iPoint.x
     yp(2) = iPoint.y
   End If 
 arc xc1,yc1*ky,rad,xp(1),yp(1),xp(2),yp(2),ky,1
 arc H-xc1,yc1*ky,rad,H-xp(2),yp(2),H-xp(1),yp(1),ky,1
End Sub 
'++++
Sub ksGetParamArc3Point(x1,y1,x2,y2,x3,y3,byref aPar)

If (x2<>x1)and(x3<>x1)Then 
  Part1 = ((X2-X1) * (X2+X1) + (Y2-Y1) * (Y2 + Y1)) /(2.0 * (X2-X1))
  Part2  = ((X3-X1) * (X3+X1) + (Y3-Y1) * (Y3 + Y1)) /(2.0 * (X3-X1))
  Part3  = (Y2 - Y1) / (X2 - X1)
  Part4  = (Y3 - Y1) / (X3 - X1)
  Yc = (Part2 - Part1) / (Part4 - Part3)
  Xc = Part2 - Part4 * Yc
  Rad = Sqr ((X3 - Xc)*(X3 - Xc) + (Y3 - Yc)*(Y3 - Yc))
 
 f1=math.ksAngle(xc,yc,x1,y1)
 f2=math.ksAngle(xc,yc,x2,y2)
 f3=math.ksAngle(xc,yc,x3,y3)

 aPar(1)=Xc
 aPar(2)=Yc
 aPar(3)=rad
 aPar(4)=f1
 aPar(5)=f2
 aPar(6)=f3
End If
 End Sub